package com.rp.system.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rp.common.utils.RpUtil;
import com.rp.log.annotation.RpLog;
import com.rp.system.dao.PreferencesDao;
import com.rp.system.po.Preferences;

@Service
@Transactional
public class PreferencesService {

	@Autowired
	private PreferencesDao preferencesDao;
	
	/**
	 * 添加参数
	 * @param pre
	 * @throws Exception 
	 */
	@RpLog(desc="添加参数")
	public void addPreferences(Preferences pre) throws Exception{
		if(RpUtil.isNullOrEmpty(pre.getPreName())){
			throw new Exception("参数名称不能为空");
		}
		List<Preferences> tmpPres = preferencesDao.find("from Preferences where preName = ?", pre.getPreName());
		if(tmpPres.size() > 0){
			throw new Exception("数据库中已存在名称为："+pre.getPreName()+"，的参数，请重新填写");
		}
		pre.setCreateDate(RpUtil.getCurrentDatetime());
		pre.setEnabled2("1");
		preferencesDao.save(pre);
	}
	
	@RpLog(desc="更新参数")
	public void updatePreferences(Preferences pre){
		pre.setUpdateDate(RpUtil.getCurrentDatetime());
		preferencesDao.update(pre);
	}
	
	@RpLog(desc="删除参数")
	public void deletePreferences(String preId){
		preferencesDao.delete(preId);
	}
	
	public void test(){
		//JdbcTemplate jdbcTemplate = (JdbcTemplate) RpUtil.getBean("");
		DriverManagerDataSource ds = new DriverManagerDataSource();
		ds.setDriverClassName("");
		ds.setUrl("");
		ds.setUsername("");
		ds.setPassword("");
		JdbcTemplate jdbcTemplate = new JdbcTemplate();
		jdbcTemplate.setDataSource(ds);
	}
	
	
	public PreferencesDao getPreferencesDao() {
		return preferencesDao;
	}
	
}
